﻿<%@ Page Language="C#" MasterPageFile="../../main.master" Inherits="VioletDevelopmentLaboratory.Net.Web.PageBase" %>

<%@ Register src="modify.ascx" tagname="modify" tagprefix="uc1" %>

<script runat="server">
	private Board currentBoard;
	
	protected void Page_Load(Object sender, EventArgs e)
	{
		if (Request.UrlReferrer == null)
		{
			Response.Redirect("list.aspx");
		}

		currentBoard = Board.GetBoard(Request.QueryString["id"]);

		if(!Page.IsPostBack)
			SysopGridViewBind();
	}

	protected void SysopGridViewBind()
	{
		SysopGridView.DataSource = currentBoard.SysopCollection;
		SysopGridView.DataBind();
	}
	
	protected void AddSysopButton_Click(Object sender, EventArgs e)
	{
		VBoardUserInfo user = UserInfoBridge.GetUserInfo(UserNameTextBox.Text);

		if (String.IsNullOrEmpty(user.Name) || String.IsNullOrEmpty(user.ProviderUserKey))
		{
			VUtility.ShowAlert(Page, UserNameTextBox.Text + "은(는) 존재하지 않는 아이디 입니다.");
			UserNameTextBox.Text = "";
			return;
		}

		SysopCreateResult result = Sysop.Add(currentBoard.ComponentID, user.ProviderUserKey, user.Name);

		switch (result)
		{
			case SysopCreateResult.Success :
				currentBoard.ModifyPageUrl(currentBoard.PageUrl);

				Response.Redirect(Request.RawUrl);
				
				UserNameTextBox.Text = "";
				break;
			case SysopCreateResult.Exists :
				VUtility.ShowAlert(Page, UserNameTextBox.Text + "은(는) 이미 시샵으로 추가되어 있습니다.");
				break;
			case SysopCreateResult.Failure:
				VUtility.ShowAlert(Page, "알 수 없는 ComponentID입니다.");
				break;
		}
	}

	protected void SysopGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
	{
		switch (Sysop.Remove(currentBoard.ComponentID, SysopGridView.DataKeys[e.RowIndex].Value.ToString()))
		{
			case SysopDeleteResult.Success :
				currentBoard.ModifyPageUrl(currentBoard.PageUrl);

				Response.Redirect(Request.RawUrl);
				break;
			case SysopDeleteResult.UnknownSysop :
				VUtility.ShowAlert(Page, "알 수 없는 시샵입니다.");
				break;
			case SysopDeleteResult.Failure :
				VUtility.ShowAlert(Page, "오류");
				break;
		}
	}
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="BodyContentPlaceHolder" runat="server">

	<h2>게시판 &gt; 목록 &gt; <%= String.Format("{0} ({1})", currentBoard.ComponentID, currentBoard.Name) %> &gt; 시샵</h2>

	<uc1:modify ID="modify1" runat="server" />

	<fieldset>
		<legend>시샵관리</legend>
		<table border="1" cellspacing="0" cellpadding="5" rules="all" style="border-collapse : collapse">
			<tr>
				<td>
					아이디
				</td>
				<td>
					<asp:TextBox ID="UserNameTextBox" runat="server" />
					<asp:RequiredFieldValidator ID="UserNameTextBoxRequiredFieldValidator" runat="server"
						ControlToValidate="UserNameTextBox" Display="Dynamic" ErrorMessage="*" SetFocusOnError="True"></asp:RequiredFieldValidator>
				</td>
			</tr>
			<tr>
				<td colspan="2">
					<asp:Button ID="AddSysopButton" runat="server" Text="추가" OnClick="AddSysopButton_Click" />
				</td>
			</tr>
		</table>
		<asp:GridView ID="SysopGridView" runat="server" AutoGenerateColumns="False" 
			DataKeyNames="providerUserKey" 
			Width="100%" PageSize="20" EmptyDataText="시샵이 없습니다." 
			onrowdeleting="SysopGridView_RowDeleting">
			<Columns>
				<asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True" />
				<asp:BoundField DataField="ProviderUserKey" HeaderText="ProviderUserKey" ReadOnly="True" />
				<asp:TemplateField ShowHeader="False">
					<ItemTemplate>
						<asp:Button ID="Button1" runat="server" CausesValidation="False" 
							CommandName="Delete" Text="제거" OnClientClick="<%$ Resources:VGlobalResources, General_Script_RemoveConfirm %>" />
					</ItemTemplate>
					<ItemStyle HorizontalAlign="Center" />
				</asp:TemplateField>
			</Columns>
		</asp:GridView>
	</fieldset>
    
</asp:Content>